Branch data Line data Source code
1 : : /**@file 2 : : * This header file is part of the I/O library; it contains the C++ interface 3 : : * for the I/O polling interface for POSIX platforms. 4 : : * 5 : : * @see lely/io2/posix/poll.h 6 : : * 7 : : * @copyright 2018-2019 Lely Industries N.V. 8 : : * 9 : : * @author J. S. Seldenthuis <jseldenthuis@lely.com> 10 : : * 11 : : * Licensed under the Apache License, Version 2.0 (the "License"); 12 : : * you may not use this file except in compliance with the License. 13 : : * You may obtain a copy of the License at 14 : : * 15 : : * http://www.apache.org/licenses/LICENSE-2.0 16 : : * 17 : : * Unless required by applicable law or agreed to in writing, software 18 : : * distributed under the License is distributed on an "AS IS" BASIS, 19 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 : : * See the License for the specific language governing permissions and 21 : : * limitations under the License. 22 : : */ 23 : : 24 : : #ifndef LELY_IO2_POSIX_POLL_HPP_ 25 : : #define LELY_IO2_POSIX_POLL_HPP_ 26 : : 27 : : #include <lely/ev/poll.hpp> 28 : : #include <lely/io2/ctx.hpp> 29 : : #include <lely/io2/event.hpp> 30 : : #include <lely/io2/posix/poll.h> 31 : : #include <lely/util/error.hpp> 32 : : 33 : : #include <utility> 34 : : 35 : : namespace lely { 36 : : namespace io { 37 : : 38 : : /** 39 : : * The system-dependent I/O polling interface. This class is a wrapper around 40 : : * `#io_poll_t*`. 41 : : */ 42 : : class Poll { 43 : : public: 44 : : /// @see io_poll_create() 45 : 5 : Poll(ContextBase& ctx, int signo = 0) : poll_(io_poll_create(ctx, signo)) { 46 [ - + ]: 5 : if (!poll_) util::throw_errc("Poll"); 47 : 5 : } 48 : : 49 : : Poll(const Poll&) = delete; 50 : : 51 : : Poll(Poll&& other) noexcept : poll_(other.poll_) { other.poll_ = nullptr; } 52 : : 53 : : Poll& operator=(const Poll&) = delete; 54 : : 55 : : Poll& 56 : : operator=(Poll&& other) noexcept { 57 : : using ::std::swap; 58 : : swap(poll_, other.poll_); 59 : : return *this; 60 : : } 61 : : 62 : : /// @see io_poll_destroy() 63 : 5 : ~Poll() { io_poll_destroy(*this); } 64 : : 65 : 17 : operator io_poll_t*() const noexcept { return poll_; } 66 : : 67 : : /// @see io_poll_get_ctx() 68 : : ContextBase 69 : : get_ctx() const noexcept { 70 : : return ContextBase(io_poll_get_ctx(*this)); 71 : : } 72 : : 73 : : /// @see io_poll_get_poll() 74 : : ev::Poll 75 : 5 : get_poll() const noexcept { 76 : 5 : return ev::Poll(io_poll_get_poll(*this)); 77 : : } 78 : : 79 : : /// @see io_poll_watch() 80 : : void 81 : : watch(int fd, Event events, struct io_poll_watch& watch, 82 : : ::std::error_code& ec) noexcept { 83 : : int errsv = errno; 84 : : errno = 0; 85 : : if (!io_poll_watch(*this, fd, static_cast<int>(events), &watch)) 86 : : ec.clear(); 87 : : else 88 : : ec = util::make_error_code(); 89 : : errno = errsv; 90 : : } 91 : : 92 : : /// @see io_poll_watch() 93 : : void 94 : : watch(int fd, Event events, struct io_poll_watch& watch) { 95 : : ::std::error_code ec; 96 : : this->watch(fd, events, watch, ec); 97 : : if (ec) throw ::std::system_error(ec, "watch"); 98 : : } 99 : : 100 : : protected: 101 : : io_poll_t* poll_{nullptr}; 102 : : }; 103 : : 104 : : } // namespace io 105 : : } // namespace lely 106 : : 107 : : #endif // !LELY_IO2_POSIX_POLL_HPP_